package com.example.highway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.CarDao;
import com.example.highway.dao.CarRecordDao;
import com.example.highway.entity.Car;
import com.example.highway.entity.CarRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@CrossOrigin
@RequestMapping("car-record")
public class CarRecordController {
    @Autowired
    CarRecordDao carRecordDao;

    @RequestMapping("add")
    public MyJson add(@RequestBody() String msg) throws ParseException {
        JSONObject jsonObject = JSON.parseObject(msg);

        CarRecord c = new CarRecord();
//        c.setrID((Integer) jsonObject.get("rID"));
        c.setcId((String)jsonObject.get("cId"));
        c.setDpNum((String)jsonObject.get("dpNum"));
        c.setAmount(Float.parseFloat(String.valueOf(jsonObject.get("amount"))));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(String.valueOf(jsonObject.get("date_format")));
        Date date = simpleDateFormat.parse(String.valueOf(jsonObject.get("aStart")));
        c.setaStart(date);
        date = simpleDateFormat.parse(String.valueOf(jsonObject.get("aEnd")));
        c.setaEnd(date);
        c.setTime((Integer) jsonObject.get("time"));
        c.setaProvince((String)jsonObject.get("aProvince"));
        c.setaProvince((String)jsonObject.get("aProof"));
        c.setpStart((String)jsonObject.get("pStart"));
        c.setpEnd((String)jsonObject.get("pEnd"));
        c.setStatus((Integer) jsonObject.get("status"));
        carRecordDao.save(c);

        return new MyJson("0", true);
    }

    @RequestMapping("get")
    public MyJson one(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

//        Optional<CarRecord> o = carRecordDao.findById((String)jsonObject.get("rID"));
//        if(!o.isPresent()){
//            return new MyJson("0", null);
//        }
//        CarRecord carRecord = o.get();

        CarRecord carRecord = jsonObject.toJavaObject(CarRecord.class);

        List<CarRecord> carRecords = carRecordDao.findAll(Example.of(carRecord));
        return new MyJson("0", JSONArray.toJSON(carRecords));

    }

//    @RequestMapping("get-all")
//    public MyJson all(){
//        List<CarRecord> o = carRecordDao.findAll();
//        if(o.isEmpty()){
//            return new MyJson("0", null);
//        }
//
//        return new MyJson("0", o);
//
//    }
}
